package com.bpc.cs.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.bpc.cs.constant.Constant;
import com.bpc.cs.dao.IRuleDAO;
import com.bpc.cs.dao.ISchemeDAO;
import com.bpc.cs.dto.SchemeDTO;
import com.bpc.cs.managed.dvo.SchemeDVO;
import com.bpc.cs.util.Message;
import com.bpc.cs.util.Util;

/**
 * @author   phan_tp
 * @datetime Nov 30, 2012 : 4:45:57 PM
 */

@Transactional(readOnly = false)
public class SchemeService implements ISchemeService {

	ISchemeDAO schemeDAO;
	IRuleDAO ruleDAO;

	/**
	 * Get all ScoringScheme record in table SCORING_SCHEME
	 * @return ArrayList<ScoringSchemeDTO>
	 */
	@Override
	public List<SchemeDVO> getAllScheme() {
		return Util.cvnSchemeDTO2DVO(getSchemeDAO().selectAllScheme());
	}

	/**
	 * Add new Scheme
	 * @param String schemeName
	 * @return Message
	 */
	@Override
	public Message addScheme(final String schemeName) {
		// Init Message object to send result back to bean
		Message msg = new Message();

		// Check the duplication of schemeName
		if (schemeDAO.isDuplicatedSchemeName(schemeName)) {
			// schemeName is duplicated
			msg.setResultBoolean(false);
			msg.setMessageKey(Constant.DUPLICATE_SCHEME_NAME);
		} else {
			// schemeName isn't duplicated
			// Insert schemeName to database
			schemeDAO.insertScheme(new SchemeDTO(schemeName));
			msg.setResultBoolean(true);
		}

		return msg;
	}

	/**
	 * Modify Scheme
	 * @param int schemeID
	 * @param String schemeName
	 * @return Message
	 */
	@Override
	public Message modifyScheme(final int schemeID, final String schemeName) {
		// Init Message object to send result back to bean
		Message msg = new Message();

		// Check scheme name is duplicate or not
		if (schemeDAO.isDuplicatedSchemeName(schemeName)) {
			// schemeName is duplicated
			msg.setResultBoolean(false);
			msg.setMessageKey(Constant.DUPLICATE_SCHEME_NAME);
		} else {
			// schemeName isn't duplicated
			// Update schemeName to database
			schemeDAO.updateScheme(new SchemeDTO(schemeID, schemeName));
			msg.setResultBoolean(true);
		}

		return msg;
	}

	/**
	 * Remove Scheme
	 * @param int schemeID
	 * @return boolean TRUE : Remove successfully
	 * 				   FALSE: Remove failed
	 */
	@Override
	public boolean removeScheme(int schemeID) {
		// Check Data in rule object before execute remove action
		return schemeDAO.countRuleOfSchemeID(schemeID) > 0 ? false : schemeDAO.deleteScheme(schemeID);
	}

	/**
	 * Remove entire scheme which contains rule, factorchoice
	 * @param int schemeID
	 * @return boolean TRUE : Remove successfully
	 * 				   FALSE: Remove failed
	 */
	@Override
	public boolean removeEntireScheme(int schemeID) {
		return schemeDAO.deleteScheme(schemeID);
	}

	/**
	 * Get ScoringSchemeDAO
	 * @return IScoringSchemeDAO
	 */
	public ISchemeDAO getSchemeDAO() {
		return schemeDAO;
	}

	/**
	 * Set ScoringSchemeDAO
	 * @param schemeDAO IScoringSchemeDAO to set
	 */
	public void setSchemeDAO(ISchemeDAO schemeDAO) {
		this.schemeDAO = schemeDAO;
	}

	/**
	 * @return the ruleDAO
	 */
	public IRuleDAO getRuleDAO() {
		return ruleDAO;
	}

	/**
	 * @param ruleDAO the ruleDAO to set
	 */
	public void setRuleDAO(IRuleDAO ruleDAO) {
		this.ruleDAO = ruleDAO;
	}

}
